package com.breaking.second.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.breaking.second.R;
import com.breaking.second.activity.pager.adapter.FragmentStateAdapter;
import com.breaking.second.activity.pager.collect.CollectPager;
import com.breaking.second.activity.pager.home.HomePager;
import com.breaking.second.activity.pager.my.MyPager;
import com.breaking.second.activity.pager.news.NewsPager;
import com.breaking.second.databinding.ActivityPagerBinding;
import com.breaking.second.databinding.DialogPagerBinding;

import java.util.ArrayList;
import java.util.List;

//首页分页
public class PagerActivity extends BaseActivity<ActivityPagerBinding> {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPager();
        initBottomNav();
        initListener();
    }

    private void initPager() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomePager());
        fragments.add(new NewsPager());
        fragments.add(new CollectPager());
        fragments.add(new MyPager());
        FragmentStateAdapter fragmentStateAdapter = new FragmentStateAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        binding.pager.setAdapter(fragmentStateAdapter);
        binding.pager.setOffscreenPageLimit(fragments.size());
        binding.pager.setUserInputEnabled(false);
    }

    private void initBottomNav() {
        binding.navView.setOnItemSelectedListener(this::onNavigationItemSelected);
    }

    private void initListener() {
        binding.fab.setOnClickListener(this::animateFab);
    }

    public void animateFab(View view) {
        DialogPagerBinding binding = DialogPagerBinding.inflate(getLayoutInflater());
        binding.addTeam.setOnClickListener(this::addTeam);
        binding.addSava.setOnClickListener(this::addSava);
        AlertDialog.Builder builder = new AlertDialog.Builder(this, androidx.appcompat.R.style.Base_Theme_AppCompat_Light_Dialog);
        AlertDialog dialog = builder.create();
        dialog.show();
        Window window = dialog.getWindow();
        if (window == null) {
            dialog.dismiss();
            return;
        }
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setContentView(binding.getRoot());
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; // Adjust width as needed
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; // Let height adjust to content
        window.setAttributes(layoutParams);
    }

    private void addSava(View view) {
        Intent intent = new Intent(this, AddGoodsActivity.class);
        intent.putExtra("type", 1);
        startActivity(intent);
    }

    private void addTeam(View view) {
        startActivity(new Intent(this, AddGoodsActivity.class));
    }

    private boolean onNavigationItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == R.id.navigation_home) {
            binding.pager.setCurrentItem(0);
        } else if (menuItem.getItemId() == R.id.navigation_set_meal) {
            binding.pager.setCurrentItem(1);
        } else if (menuItem.getItemId() == R.id.navigation_team) {
            binding.pager.setCurrentItem(2);
        } else if (menuItem.getItemId() == R.id.navigation_notifications) {
            binding.pager.setCurrentItem(3);
        }
        return true;
    }
}
